/******************************************************************************/
/*                                                                            */
/*  Copyright (c) 2009: ngi-central.org                                       */
/*  All rights reserved.                                                      */
/*                                                                            */
/******************************************************************************/


/// \file
/// This file implements the direction geometric primitive.

#ifndef __sgl_direction_h__
#define __sgl_direction_h__

#include <sgl/global_settings.h>
#include <sgl/conditions.h>
#include <sgl/geometric_primitive.h>
#include <sgl/real_concept.h>
#include <sgl/radians_degrees.h>
#include <boost/spirit/include/phoenix_core.hpp>
#include <boost/spirit/include/phoenix_operator.hpp>
#include <boost/static_assert.hpp>
#include <boost/operators.hpp>
#include <boost/concept_check.hpp>
#include <algorithm>

namespace sgl {


/******************************************************************************/
/*                                                                            */
/* direction                                                                  */
/*                                                                            */
/******************************************************************************/

template <
	/// The dimensionality. 
	//!
	//! This defaults to two dimensions.
	size_t D=2,
	/// The element type. 
	//! 
	//! This type defaults to SGL_FPTYPE (which in turn defaults to double), but 
	//! any other numeric type is also suitable.
	typename T=SGL_FPTYPE>
/// A direction in euclidean space.
class direction
	/// A direction is a geometric primitive.
	: public geometric_primitive<D,T>
	, boost::equality_comparable<sgl::direction<D,T>, sgl::direction<D,T>
	, boost::additive<sgl::direction<D,T>, sgl::direction<D,T>
	, boost::multiplicative<sgl::direction<D,T>, T
	> > >
{
	// A direction must have two or more dimensions.
	BOOST_STATIC_ASSERT(D >= 2);

	// Only real types qualify for type T. If this line triggers an error,
	// type T is not a real type.
	BOOST_CONCEPT_ASSERT((sgl::real_concept<T>));

public:
	/// Default constructor.
	//!
	//! Constructs a zero direction.
	direction()
	{ 
		for (size_t i=0; i<D-1; ++i)
			angles_[i] = T();
		for (size_t i=0; i<D-1; ++i)
			SGL_POSTCONDITION(angles_[i] == 0,
				"sgl::direction::angles_ is not correctly initialized.");
	}

	/// Default constructor for a two-dimensional direction.
	direction(
		/// The first element of the direction.
		T phi)
	{ 
		BOOST_STATIC_ASSERT(D == 2);

		angles_[0] = phi;
	}

	/// Default constructor for a three-dimensional direction.
	direction(
		/// The first element of the direction.
		T phi,
		/// The second element of the direction.
		T theta)
	{ 
		BOOST_STATIC_ASSERT(D == 3);

		angles_[0] = phi;
		angles_[1] = theta;
	}

	/// Copy constructor.
	direction(
		/// The right hand side.
		sgl::direction<D,T> const& rhs)
	{ 
		for (size_t i=0; i<D-1; ++i)
			angles_[i] = rhs.angles_[i];
		for (size_t i=0; i<D-1; ++i)
			SGL_POSTCONDITION(angles_[i] == rhs.angles_[i],
				"sgl::direction::angles_ is not correctly initialized.");
	}

	/// Copy assignment operator.
	sgl::direction<D,T> & operator = (
		/// The right hand side.
		sgl::direction<D,T> rhs)
	{
		swap(rhs);
		return *this;
	}

	/// Exchanges the elements of two direction objects.
	void swap(
		/// The other direction.
		sgl::direction<D,T> & rhs)
	{
		SGL_PRECONDITION(D == rhs.dimensions(),
			"dimension mismatch.");

		for (size_t i=0; i<D-1; ++i)
			std::swap(angles_[i], rhs.angles_[i]);
	}

	/// Indexing operator.
	//!
	//! /return Returns a reference to the element at the specified index.
	T & operator [] (
		/// The index into the direction (zero based) used to access the direction's 
		/// elements.
		size_t index)
	{
		SGL_PRECONDITION(index >= 0,
			"index parameter is out of range.");
		SGL_PRECONDITION(index < D-1,
			"index parameter is out of range.");

		return angles_[index];
	}

	/// Indexing operator.
	//!
	//! /return Returns a const reference to the element at the specified index.
	T const & operator [] (
		/// The index into the direction (zero based) used to access the direction's 
		/// elements.
		size_t index) const
	{
		SGL_PRECONDITION(index >= 0,
			"index parameter is out of range.");
		SGL_PRECONDITION(index < D-1,
			"index parameter is out of range.");

		return angles_[index];
	}

	/// Find the type of the geometric primitive.
	//!
	//! /return The type of the primitive.
	geometric_type type() const
	{
		return sgl::geometric_primitive<D,T>::direction;
	} 

	/// Access the first element of the direction.
	//!
	//! /return Returns a reference to the first element of the direction.
	T & phi()
	{
		BOOST_STATIC_ASSERT(D >= 2);
		return angles_[0];
	}

	/// Access the first element of the direction.
	//!
	//! /return Returns a const reference to the first element of the direction.
	T const & phi() const
	{
		BOOST_STATIC_ASSERT(D >= 2);
		return angles_[0];
	}

	/// Access the second element of the direction.
	//!
	//! /return Returns a reference to the second element of the direction.
	T & theta()
	{
		BOOST_STATIC_ASSERT(D >= 3);
		return angles_[1];
	}

	/// Access the second element of the direction.
	//!
	//! /return Returns a const reference to the second element of the 
	//! direction.
	T const & theta() const
	{
		BOOST_STATIC_ASSERT(D >= 3);
		return angles_[1];
	}

	/// Unary negation.
	//!
	//! A direction is negated by negating all its components. A negated 
	//! direction points in the opposite direction.
	//!
	//! \return A negated direction object.
    sgl::direction<D,T> operator - () const
    {
		sgl::direction<D,T> temp(*this);
		std::transform(angles_, angles_+D-1, temp.angles_, 
			boost::phoenix::arg_names::arg1 + sgl::radians(T(180)));
        return temp;
    }

	/// Equality operator.
	//!
	//! Two directions are compared for equality by comparing their components 
	//! in a pairwise manner. The directions are equal only if all pairwise 
	//! components are equal.
	//!
	//! Through the use of boost::equality as a base class operator != is 
	//! defined as well and implemented in terms of this operator ==.
	//!
	//! \return true if equal, false otherwise.
    bool operator == (
		/// Right hand side.
		sgl::direction<D,T> const& rhs) const
    {
		SGL_PRECONDITION(D == rhs.dimensions(),
			"dimension mismatch.");

		return std::equal(angles_, angles_+D-1, rhs.angles_);
    }

	/// Add two directions.
	//!
	//! Two directions are added by adding their components in a pairwise 
	//! manner.
	//!
	//! Through the use of boost::additive as a base class operator + is 
	//! defined as well and implemented in terms of this operator +=.
	//!
	//! \return A reference to this object.
    sgl::direction<D,T> & operator += (
		/// Right hand side.
		sgl::direction<D,T> const& rhs)
    {
		SGL_PRECONDITION(D == rhs.dimensions(),
			"dimension mismatch.");

		T * i = angles_;
		T const* j = rhs.angles_;
		for (; i!=angles_+D-1; ++i, ++j)
			(*i) += (*j);

        return *this;
    }

	/// Subtract two directions.
	//!
	//! Two directions are subtracted by subtracting their components in a 
	//! pairwise manner.
	//!
	//! Through the use of boost::additive as a base class operator - is 
	//! defined as well and implemented in terms of this operator -=.
	//!
	//! \return A reference to this object.
    sgl::direction<D,T> & operator -= (
		/// Right hand side.
		sgl::direction<D,T> const& rhs)
    {
		SGL_PRECONDITION(D == rhs.dimensions(),
			"dimension mismatch.");

		T * i = angles_;
		T const* j = rhs.angles_;
		for (; i!=angles_+D-1; ++i, ++j)
			(*i) -= (*j);

		return *this;
    }

	/// Scale a direction by multiplication.
	//!
	//! A direction is scaled by multiplying each of its components with the 
	//! factor.
	//!
	//! Through the use of boost::multiplicative as a base class operator * is 
	//! defined as well and implemented in terms of this operator *=.
	//!
	//! \return A reference to this object.
	sgl::direction<D,T> & operator *= (
		/// The scaling factor.
		T const& factor)
	{
		for (T * i=angles_; i!=angles_+D-1; ++i)
			(*i) *= factor;

		return *this;
	}

	/// Scale a direction by division.
	//!
	//! A direction is scaled by dividing each of its components through the 
	//! divisor.
	//!
	//! Through the use of boost::multiplicative as a base class operator / is 
	//! defined as well and implemented in terms of this operator /=.
	//!
	//! \return A reference to this object.
	sgl::direction<D,T> & operator /= (
		/// The scaling factor.
		T const& divisor)
	{
		for (T * i=angles_; i!=angles_+D-1; ++i)
			(*i) /= divisor;

		return *this;
	}

private:
	// The components of the direction.
	T angles_[D-1];
};


/// A two-dimensional direction with the default component type.
typedef sgl::direction<2> direction2;

/// A two-dimensional direction with float component type.
typedef sgl::direction<2, float> direction2f;

/// A two-dimensional direction with double component type.
typedef sgl::direction<2, double> direction2d;


/// A three-dimensional direction with the default component type.
typedef sgl::direction<3> direction3;

/// A three-dimensional direction with float component type.
typedef sgl::direction<3, float> direction3f;

/// A three-dimensional direction with double component type.
typedef sgl::direction<3, double> direction3d;


} // namespace sgl

#endif // __sgl_direction_h__
